<?php
echo $this->Html->css('form');
echo $this->Html->script('jquery.validate');
echo $this->Html->script('jquery.metadata');
?>
<div class="product-box">
<h3><?php echo __('Thông tin đơn hàng', true); ?></h3>
<?php echo $form->create(array(
		'id'			=> 'formValidation',
		'action' 		=> 'index',
		'enctype' 		=> 'multipart/form-data',
		'autocomplete'	=> 'off'));
	?>
	<h4><?php echo __('Thông tin sản phẩm', true);?></h4>
	<?php
		if ($basket_kind == 0) {
	?>
	 <div class="input">
		<label><?php echo __('Loại sản phẩm', true); ?></label>
		<span id="load-product_types">
		<?php
			echo $form->input(
						'Order.product_type',
						array(
							'type'		=> 'select',
							'label'		=> false,
							'div'		=> false,
							'options'	=> $product_types,
							'name'      => 'product_types',
							'id'      => 'product_types'
						)
					);
		?>
		</span>
	</div>
	<div class="input">
		<label><?php echo __('Sản phẩm', true); ?></label>
		<span id="load_products">
		<?php
			echo $form->input(
						'Order.product',
						array(
							'type'		=> 'select',
							'label'		=> false,
							'div'		=> false,
							'options'	=> $products,
							'name'      => 'products'
						)
					);
		?>
		</span>
	</div>
	<?php
		echo $form->input(
					'Order.quantity',
					array(
						'type'		=> 'text',
						'label'		=> __('Số lượng (kg)', true),
						'after'		=> '<span class="require">*</span>',
						'div'		=> true,
						'size'		=> 50,
						'validate'	=> 'required:true,email:true'
					)
				);
		}
		else {
	?>
	<table class="table table-striped">
            <thead>
                <tr>
                    <th><?php echo __('#', true); ?></th>
                    <th><?php echo __('Product name', true); ?></th>
                    <th width="100"><?php echo __('Quantity', true); ?></th>
                    <th><?php echo __('Price', true); ?>(VNĐ)</th>
                    <th><?php echo __('Total', true); ?>(VNĐ)</th>
                    <th><?php echo __('Update', true); ?> </th>
                </tr>
            </thead>
            <tbody>
                <?php 
                    $i = 1;
                    $total = 0;
                    foreach($orders as $item) { 
				$sub_total = $item['price'] * $item['quantity'];
				$total += $sub_total;
		?>
                <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $item['name_'.$lang]; ?></td>
                    <td><input type="text" id="quantity<?php echo $item['id']; ?>" value="<?php echo $item['quantity']; ?>" size="3" class="input-mini"/></td>
                    <td><?php echo number_format($item['price'],0,' ','.'); ?></td>
                    <td>
                        <?php if (isset($vipDiscount)) : ?>
                            <?php echo number_format($sub_total,0,' ','.'); ?>
                        <?php else :
                            echo number_format($sub_total * (100-$vipDiscount)/100,0,' ','.');
                            endif;
                        ?>
                    </td>
                    <td><input value="<?php echo __('Update', true); ?>" type="button" onclick="run_update('update',<?php echo $item['id']; ?>);" />
					<input value="<?php echo __('Delete', true); ?>" type="button" onclick="run_update('delete',<?php echo $item['id']; ?>);" /> </td>
                </tr>
                <?php $i++;?>
                <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td><strong><?php echo "Tổng cộng"; ?></td>
                    <td><strong><?php echo number_format($total,0,' ','.'); ?></strong></strong></td>
                    <td></td>
                </tr>
                
            </tfoot>
        </table>
	<div class="cleaner"></div>
	<?php
		}
	?>
	<h4><?php echo __('Thông tin đặt hàng', true);?></h4>
	<?php
			echo $form->input(
				'Order.bill_name',
				array(
					'type'		=> 'text',
					'label'		=> __('Fullname', true),
					'after'		=> '<span class="require">*</span>',
					'div'		=> true,
					'size'		=> 50,
					'validate'	=> 'required:true',
					'value'     => (!empty($login['User']) ? $login['User']['fullName'] : NULL)
				)
			);
			echo $form->input(
				'Order.bill_address',
				array(
					'type'		=> 'text',
					'label'		=> __('Address', true),
					'after'		=> '<span class="require">*</span>',
					'div'		=> true,
					'size'		=> 50,
					'validate'	=> 'required:true',
					'value'     => (!empty($login['User']) ? $login['User']['address'] : NULL)
				)
			);
			echo $form->input(
				'Order.bill_phone',
				array(
					'type'		=> 'text',
					'label'		=> __('Phone', true),
					'after'		=> '<span class="require">*</span>',
					'div'		=> true,
					'size'		=> 50,
					'validate'	=> 'required:true',
					'value'     => (!empty($login['User']) ? $login['User']['phone'] : NULL)
				)
			);

	?>
	<h4><?php echo __('Thông tin giao hàng', true);?></h4>
	<?php
			echo $form->input(
				'Order.delivery_name',
				array(
					'type'		=> 'text',
					'label'		=> __('Fullname', true),
					'after'		=> '<span class="require">*</span>',
					'div'		=> true,
					'size'		=> 50,
					'validate'	=> 'required:true',
					'value'     => (!empty($login['User']) ? $login['User']['fullName'] : NULL)
				)
			);
			echo $form->input(
				'Order.delivery_address',
				array(
					'type'		=> 'text',
					'label'		=> __('Address', true),
					'after'		=> '<span class="require">*</span>',
					'div'		=> true,
					'size'		=> 50,
					'validate'	=> 'required:true',
					'value'     => (!empty($login['User']) ? $login['User']['address'] : NULL)
				)
			);
			echo $form->input(
				'Order.delivery_phone',
				array(
					'type'		=> 'text',
					'label'		=> __('Phone', true),
					'after'		=> '<span class="require">*</span>',
					'div'		=> true,
					'size'		=> 50,
					'validate'	=> 'required:true',
					'value'     => (!empty($login['User']) ? $login['User']['phone'] : NULL)
				)
			);
			echo $form->input(
				'Order.note',
				array(
					'type'		=> 'textarea',
					'label'		=> __('Notes', true),
					'div'		=> true,
					'cols'		=> 100
				)
			);

	?>
	<div class="input">
				<label><?php echo __('Security Code', true); ?></label>
				<?php
				echo $form->input(
					'Security.code',
					array(
						'type'		=> 'text',
						'label'		=> false,
						'div'		=> false,
						'size'		=> 6,
						'validate'	=> 'required:true',
						'value'		=> ''
					)
				);
				?>
				<img src="<?php echo $captcha_image_url; ?>" id="captcha" alt="CAPTCHA Image" />
				<?php
					echo "<br/>";
					echo (!empty($error_captcha)) ? '<span class="status">'.$error_captcha.'</span>' : '';
				?>
			</div>
	<div class="submit">
		<?php
		echo $form->input(
					'Order.member',
					array(
						'type'		=> 'hidden',
						'value'		=> (!empty($login['User']) ? $login['User']['id'] : NULL)
					)
				);
		?>
		<?php echo $form->button(__('Send', true), array('class'=>'h30x137 accept', 'type'=>'submit', 'id'=>'regSubmit')); ?>
		<?php echo $form->button(__('Change', true), array('class'=>'h30x137 reset', 'type'=>'reset')); ?>
	</div>
</form>
<script type="text/javascript">
	<!--
	$(document).ready(
		function()
		{
			$.metadata.setType("attr", "validate");
			$("form#formValidation").validate();

			$("#product_types").change
			(
				function()
				{
					$('#load_products').html('<?php echo $html->image('loading.gif') . __('Waiting to loading...', true); ?>');
					$.ajax
					({
						url: '<?php echo Router::url(array('plugin' => null, 'controller' => 'auto', 'action' => 'load_products'), true); ?>',
						type: 'POST',
						data: {product_type:$("#product_types").val()},
						success: function(respsonse)
						{
							$('#load_products').html(respsonse);
						}
					});
				}
			);
		}
	);
	//-->
</script>
</div>